<?php

namespace app\models;

use think\Model;

/**
 * Class CampusModel.
 */
class AuditionModel extends Model
{
    protected $table = 'edu_audition_record';

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     * @return string
     */
    public static function getIds()
    {
        $campus = self::select()->toArray();
        $arr = [];
        foreach ($campus as $cap) {
            $arr[] = $cap['id'];
        }

        return implode(',', $arr);
    }

    /**
     * @param $id
     * @param string $type
     *
     * @throws \think\exception\DbException
     *
     * @return null|array|AuthGroupAccessModel
     */
    public static function getById($id, $type = 'all')
    {
        if (!is_numeric($id)) {
            return [];
        }
        $campus = self::get(['id' => $id]);
        if ($campus) {
            return (isset($campus[$type])) ? $campus[$type] : $campus;
        }

        return [];
    }

    /**
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getAuditionLists($param)
    {
        $StudentModel = new StudentModel();
        $GradesDetailsListsModel = new GradesDetailsListsModel();
        $GradesDetailsModel = new GradesDetailsModel();  // 班级
        $StudentSubjectClassModel = new StudentSubjectClassModel();   // 学生

        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        $where_ = [];
        $where__ = [];



        if (isset($param['keywords']) && '' !== $param['keywords']) {
            if(isset($param['stype'])){
                if ($param['stype'] == ''){
                    $where_['student_name|name'] = ['like', $param['keywords'].'%'];
                }elseif ($param['stype'] == '2'){
                    $where_['phone'] = $param['keywords'];
                }elseif ($param['stype'] == '3'){
                    $where_['father_name|mother_name'] = ['like', $param['keywords'].'%'];
                }elseif ($param['stype'] == '4'){
                    $where_['father_phone|mother_phone'] = $param['keywords'];
                }
            }
        }


        if (isset($param['user_id']) && '' !== $param['user_id']) {
            $where_['user_id'] = $param['user_id'];
        }

        if(get_current_type() != 1){
            if(get_current_type() == 4 || get_current_type() == 5){
                $where['campus_id'] = get_current_campus_id();
            }elseif (get_current_type() == 2 || get_current_type() == 3){
                $where['campus_id'] = ['in',get_current_campus_id()];
            }
        }

        if (isset($param['select_campus_audition']) && '' !== $param['select_campus_audition']) {   //选择校区
            $where['campus_id'] = $param['select_campus_audition'];
        }


        if (isset($param['user_id_audition']) && '' !== $param['user_id_audition']) {   //选择课程顾问
            $where_['user_id'] = $param['user_id_audition'];
        }


        if (isset($param['door_state_audition']) && '' !== $param['door_state_audition']) {
            if($param['door_state_audition'] == 1){  //已跟进
                $where_['follow_up'] = 35;
            }else if($param['door_state_audition'] == 2){  //已上门
                $where_['follow_up'] = 103;
            }
        }

        if (isset($param['channel_audition']) && '' !== $param['channel_audition']) {  //来源
                $where_['channel'] = $param['channel_audition'];
        }

        if (isset($param['grades_class_audition']) && '' !== $param['grades_class_audition']) {  //班级
            $where__['class_manage_id'] = $param['grades_class_audition'];
        }

        if (isset($param['class_room_audition']) && '' !== $param['class_room_audition']) {  //教室
            $where__['class_room_id'] = $param['class_room_audition'];
        }


        if (isset($param['teachers_audition']) && '' !== $param['teachers_audition']) {  //老师
            $where__['teacher'] = ['like', '%'.$param['teachers_audition'].'%'];
        }

        if (isset($param['day']) && '' !== $param['day']) {
            $date = explode(' - ', $param['day']);
            $start = $date[0];
            $end = $date[1];
            $where__['class_date'] = [['egt', $start], ['elt', $end]];
        }


        if (isset($param['sign_status']) && '' !== $param['sign_status']) {  //签到
            $where['sign_status'] = $param['sign_status'];
        }


        if(count($where__) > 0){
            $res = $GradesDetailsListsModel->where($where__)->column('id');
            $res_str = implode(',',$res);
            $where['class_id'] = ['in',$res_str];
        }

        if(count($where_) > 0){
            $res = $StudentModel->where($where_)->column('id');
            $res_str = implode(',',$res);
            $where['student_id'] = ['in',$res_str];
        }


        $listData = self::where($where)->order('update_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                    $res_student = $StudentModel::get(['id' => $item['student_id']]);
                    $res_class = $GradesDetailsListsModel::get(['id' => $item['class_id']]);
                    if($item['status'] == 0){
                        $item['describe'] = '无效';
                    }else if($item['status'] == 1){
                        $item['describe'] = '有效';
                    }
                    $item['student_name'] =  $res_student['name'];
                    $item['student_phone'] = $res_student['phone'];
                    $item['channel'] = DictionaryModel::get($res_student['channel'])['name'];
                    $item['audition_grades'] = $res_class['class_grades_name'];
                    $item['class_time'] = $res_class['class_date'].$res_class['weekday'].$res_class['class_subject_start'].'-'.$res_class['class_subject_end'];
                    $item['teacher'] = $res_class['teacher'];
                    $item['class_room'] = $res_class['class_room'];
                    $item['course_consultant'] = $res_student['course_consultant'];
                    $item['follow_up_text'] =DictionaryModel::get($res_student['follow_up'])['name'];
            }
        }

        return $listData;
    }


}
